home *** CD-ROM | disk | FTP | other *** search
- Path: news.gate.net!news-adm
- From: Howard Gardner <hgardner@gate.net>
- Newsgroups: comp.lang.c++
- Subject: Pointer Comparison
- Date: Tue, 02 Apr 1996 11:37:57 -0500
- Organization: CyberGate, Inc.
- Message-ID: <316157E5.4180@gate.net>
- NNTP-Posting-Host: jaxfl2-22.gate.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
-
- Consider this function:
-
- int isLess
- (
- const char * left,
- const char * right
- )
- {
-
- return ( left < right ); // Compare POINTERS, not values
-
- }
-
- I believe that the result of this function is officialy undefined. I am
- trying
- to determine whether depending on it is merely unsafe, or is downright
- foolhardy. It seems a statistical approach is about the only way to
- determine
- this. Here are the assertions I need to test:
-
- 1) No compiler exists for which this function will not
- compile.
- 2) No compiler exists for which isLess( a, b ) and
- isLess( b, a ) will both return non-zero.
- 3) No compiler exists for which isLess( a, a ) will
- return non-zero.
-
- For a compiler on which these assumptions are correct, the
- program below will print:
-
- OK
- OK
-
- If the assumptions are not correct, the program may not compile, or
- it will print something else.
-
- I would appreciate it if everyone tried it with their compiler, and
- reported
- the results via e-mail or comp.lang.c++.moderated. Please mention the
- compiler's name and version.
-
- // Cut here
- #include <iostream.h>
-
- int isLess
- (
- const char * left,
- const char * right
- )
- {
-
- return ( left < right ); // Compare POINTERS, not values
-
- }
-
- int main
- (
- int,
- char **
- )
- {
-
- char lAVal;
- char lBVal;
-
- char * lAPtr = &lAVal;
- char * lBPtr = &lBVal;
-
- if ( isLess ( lAPtr, lBPtr ) )
- {
-
- if ( isLess ( lBPtr, lAPtr ) )
- {
-
- cout << "ERROR" << endl;
-
- }
- else
- {
-
- cout << "OK" << endl;
-
- }
-
- }
-
- else if ( isLess ( lBPtr, lAPtr ) )
- {
-
- if ( isLess ( lAPtr, lBPtr ) )
- {
-
- cout << "ERROR" << endl;
-
- }
-
- else
- {
-
- cout << "OK" << endl;
-
- }
-
- }
-
- if ( isLess( lAPtr, lAPtr ) )
- {
-
- cout << "ERROR" << endl;
-
- }
- else
- {
-
- cout << "OK" << endl;
-
- }
-
- }
-